*' --------------
* sameness effects: main effects ----
*' ==============

clear all
* set up your working directory here
global home_dir ""

cd ${home_dir}

* predicted margins for interaction
cap: program drop margin_main
program margin_main
	args X k mname year vv geo_type
	sum `X', d 
	local gap = (`r(max)' - `r(min)') / `k' 
	margin if e(sample), at(`X' = (`r(min)' (`gap') `r(max)')) predict(pr) post
	
	matrix A = r(table)
	matrix B = r(at)

	mat2txt, matrix(A) saving("./output/margins/`vv'_margins_main_`mname'_`X'_`year'_`geo_type'.txt") replace
	mat2txt, matrix(B) saving("./output/margins/`vv'_at_main_`mname'_`X'_`year'_`geo_type'.txt") replace

end 

cap: program drop margin_interact
program margin_interact 
	args X Y k mname year vv geo_type
	sum `X', d 
	local gap = (`r(max)' - `r(min)') / `k' 
	margin `Y' if e(sample), at(`X' = (`r(min)' (`gap') `r(max)')) predict(pr) post
	
	matrix A = r(table)
	matrix B = r(at)

	mat2txt, matrix(A) saving("./output/margins/`vv'_margins_interact_`mname'_`X'_`Y'_`year'_`geo_type'.txt") replace
	mat2txt, matrix(B) saving("./output/margins/`vv'_at_interact_`mname'_`X'_`Y'_`year'_`geo_type'.txt") replace

end 

cap: program drop run_figure2
program run_figure2

args geo_type

use "./data/processed_notshared/cleaned_regtable_unemployment_`geo_type'_v2.dta", clear 

local vv "v2"
local year "all"

svyset [pw=weight] 	

if ("`vv'" == "v1"){
	keep if sample_v1 == 1	
}
if ("`vv'" == "v2") {
	keep if sample_v2 == 1	
}

local demographics_individual Female agegroup Race6 BornUSA  
local demographics_county RAT_Female RAT_AgeGrp4_2 RAT_AgeGrp4_3 RAT_AgeGrp4_4 RAT_Race6_2 RAT_Race6_3 RAT_Race6_4 RAT_Race6_5 RAT_Race6_6 RAT_BornUSA 
local contextual_control pop_density

egen n_miss = rowmiss(Suic St `geo_type' Year `demographics_individual' `demographics_county' `contextual_control')
recode n_miss (0=0) (1/max=1)

drop if n_miss > 0

local eq_demographics_individual Female i.agegroup i.Race6 BornUSA 
local eq_demographics_county RAT_Female RAT_AgeGrp4_2 RAT_AgeGrp4_3 RAT_AgeGrp4_4 RAT_Race6_2 RAT_Race6_3 RAT_Race6_4 RAT_Race6_5 RAT_Race6_6 RAT_BornUSA 
local eq_contextual_control pop_density

local model_eq `eq_demographics_individual' `eq_demographics_county' `eq_contextual_control' 

estimates clear 
logit Suic i.St `model_eq' RAT_UnEmpl [pw=weight], cluster(`geo_type'), if empstat == 2
estimates store est1_inter_unemp

logit Suic i.St `model_eq' RAT_Empl  [pw=weight], cluster(`geo_type'), if empstat == 1
estimates store est1_inter_emp

logit Suic i.St `model_eq' RAT_NotInLabor  [pw=weight], cluster(`geo_type'), if empstat == 3
estimates store est1_inter_nlf

logit Suic i.St `model_eq' b2.empstat c.std_same_prop_empstat b2.empstat#c.std_same_prop_empstat [pw=weight], cluster(`geo_type')
estimates store est1_same_inter

esttab est1_inter_unemp est1_inter_emp est1_inter_nlf est1_same_inter using "./output/table_logit_v2_`geo_type'.csv", csv replace label nogap se star (+ 0.1 * 0.05 ** 0.01) 

* for intearction effects
estimates restore est1_inter_unemp
margin_main RAT_UnEmpl 20 "est1main_unemp" `year' `vv' `geo_type'

estimates restore est1_inter_emp
margin_main RAT_Empl 20 "est1main_emp" `year' `vv' `geo_type'

estimates restore est1_inter_nlf
margin_main RAT_NotInLabor 20 "est1main_nlf" `year' `vv' `geo_type'

* predicted margins for interaction
estimates restore est1_same_inter
margin_interact std_same_prop_empstat empstat 20 "est1sameinter" `year' `vv' `geo_type'

* here, focus on comparison of effect sizes : table 1
recode empstat (1=2 "Employed") (2=1 "Unemployed") (3=3 "NLF"), gen(empstat2)

log using "./output/margins/table1_comparison_`geo_type'.txt", replace text 
logit Suic i.St `model_eq' b1.empstat2 c.std_same_prop_empstat b1.empstat2#c.std_same_prop_empstat [pw=weight], cluster(`geo_type')
estimates store tempabc

di "comparison of the effect sizes against unemployed population at the level of `geo_type'"
estimates restore tempabc
margins empstat2, dydx(std_same_prop_empstat) predict(pr) pwcompare

estimates restore tempabc
margins, at(empstat2 = (1 2 3)) dydx(std_same_prop_empstat) predict(pr) post
log close

end

run_figure2 cz
run_figure2 county

